﻿CREATE PROCEDURE [dbo].[wikiPages_get_wikiPages_by_wikiCategory]
    @page int = 0,
    @size int = 0,
    @column nvarchar(256),
    @direction int = 0,
    @wikicategoryName nvarchar(256)
AS
    SELECT 
    WikiPages.PageId, 
    WikiPages.CategoryId, 
    WikiPages.Name, 
    Content, 
    CreationDate, 
    PublicUrl 
    From WikiPages
    inner join WikiCategories on WikiPages.CategoryId = WikiCategories.CategoryId
    Where WikiCategories.Name = @wikicategoryName

ORDER BY
    CASE WHEN @column = 'Name' AND @direction = 1 THEN WikiPages.Name END DESC,
    CASE WHEN @column = 'Name' AND @direction = 0 THEN WikiPages.Name END ASC,
    CASE WHEN @column = 'CreationDate' AND @direction = 1 THEN WikiPages.CreationDate END DESC,
    CASE WHEN @column = 'CreationDate' AND @direction = 0 THEN WikiPages.CreationDate END ASC

OFFSET ((@page-1)*@size) ROWS
FETCH NEXT @size ROWS ONLY;

RETURN 0;
